package com.usefullc.user.controller.admin;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.common.enums.ValidActionEnum;
import com.usefullc.system.validator.UsefulValid;
import com.usefullc.user.domain.Tenant;
import com.usefullc.user.param.TenantParam;
import com.usefullc.user.vo.TenantVo;
import com.usefullc.user.query.TenantQuery;
import com.usefullc.user.service.TenantService;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdsParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import javax.validation.Valid;

import static com.usefullc.system.controller.WebResultBuilder.successResult;

/**
 * 租户控制类
 * @author: 星空
 * @date: 2022-09-01
 */
@RestController
@RequestMapping("/admin/tenant/")
@Api( tags="租户")
public class TenantController extends BaseController {

    @Autowired
    private TenantService tenantService;

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "租户列表",response = WebResult.class)
    @GetMapping(path = "list",produces = "application/json")
    public WebResult list(@UsefulValid(query = true) TenantQuery query) {
        Pager<TenantVo> page = tenantService.findVoByPage(query);
        return WebResultBuilder.successResult(page);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "租户所有列表", response = WebResult.class)
    @GetMapping(path = "listAll", produces = "application/json")
    public WebResult listAll(@UsefulValid(query = true) TenantQuery query) {
        List<Tenant> dataList = tenantService.findAll(query);
        return WebResultBuilder.successResult(dataList);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "租户详情",response = WebResult.class)
    @GetMapping(path = "detail",produces = "application/json")
    public WebResult detail(@ApiParam(value = "示例ID") @Valid @RequestParam Long id) {
        Tenant tenant = tenantService.findById(id);
        return WebResultBuilder.successResult(tenant);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "创建租户", response = WebResult.class)
    @PostMapping(path = "/save", produces = "application/json")
    @UsefulValid
    public WebResult save(@UsefulValid(save=true) @RequestBody TenantParam param) {
        Tenant tenant = BeanUtils.beanCopy(param, Tenant.class);
        return successResult(tenantService.save(tenant));
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "修改租户", response = WebResult.class)
    @PostMapping(path = "/update", produces = "application/json")
    public WebResult update(@UsefulValid(update=true) @RequestBody TenantParam param) {
        Tenant oldBean = tenantService.findById(param.getId());
        BeanUtils.beanCopy(param, oldBean);
        return successResult(tenantService.update(oldBean));
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "del",produces = "application/json")
    @ApiOperation(value = "删除租户",response = WebResult.class)
    public WebResult del(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        tenantService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "delByLogic",produces = "application/json")
    @ApiOperation(value = "逻辑删除租户",response = WebResult.class)
    public WebResult delByLogic(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        tenantService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }






}
